package com.example.zsgc;

import com.example.zsgc.dao.UserMapper;
import com.example.zsgc.entity.People;
import com.example.zsgc.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("user")
@SpringBootApplication
public class ZsgcApplication {
    @Autowired
    UserMapper userMapper;


    public static void main(String[] args) {
        SpringApplication.run(ZsgcApplication.class, args);
    }

    //增加
    @RequestMapping("save")
//   @ResponseBody
    public String save(User user) {
        userMapper.save(user);
        return "redirect:/user/listhtml";
    }

    //删除
    @RequestMapping("delete/{id}")
//@ResponseBody
    public String delete(@PathVariable("id") String xxx) {
        userMapper.delete(xxx);
        return "redirect:/user/listhtml";
    }


    //查询(直接简单显示)
    @RequestMapping("chaxun")
    @ResponseBody
    public List<User> list() {
        List<User> list = userMapper.list();
        return list;
    }

    //
    @RequestMapping("listhtml")
    public String listHtml(ModelMap map) {
        List<User> list = userMapper.list();
        map.put("userList", list);
//        for (User user : list) {
//            System.out.println(user.getId());
//            System.out.println(user.getName());
//            System.out.println(user.getAge());
//        }
        return "/user/userlist";
    }

    @RequestMapping("add")
    public String add() {

        return "/user/add";
    }

    @RequestMapping("edit/{id}/{name}/{age}")
    public String edit(@PathVariable("id") String id,
                       @PathVariable("name") String name,
                       @PathVariable("age") String age,
                       ModelMap map) {

        User user = userMapper.select(id);
        map.put("user", user);
        return "/user/update";
    }

    //修改
    @RequestMapping("update")
//    @ResponseBody
    public String update(User user) {
        userMapper.update(user);
        return "redirect:/user/listhtml";
    }

    @RequestMapping("index")
    public String index() {
        return "/user/index";
    }

    @PostMapping("post")
    public String post(People people) {
        if (userMapper.lists(people.getUsername()) != null) {
            People people1 = userMapper.lists(people.getUsername());
            People people2 = people;
            if (people.getPassword() .equals(people1.getPassword())) {
                //登录成功返回的页面
                return "redirect:/user/HomePage";
            }
        }
        return "redirect:/user/index";
    }

@RequestMapping("HomePage")
    public String homepage(){
        return "/user/HomePage";
}

@RequestMapping("one")
    public String one(){
        return "/user/one";
}
    @RequestMapping("culture")
    public String culture(){
        return "/user/culture";
    }

    @RequestMapping("shopping")
    public String shopping(){
        return "/user/shopping";
    }

    @RequestMapping("cate")
    public String cate(){
        return "/user/cate";
    }

    @RequestMapping("overview")
    public String overview(){
        return "/user/overview";
    }
}
